<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                template="./templates/templateHome.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:p="http://primefaces.org/ui"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:define name="title">
        Reserva
    </ui:define>

    <ui:define name="content">

        <p:growl id="growlReserva" life="5000" showDetail="true"/>

        <h:form rendered="#{(loginBean.exibeFuncaoAdm) or (loginBean.exibeFuncaoReserva)}">
            <p:commandButton id="btnCriarReserva" title="Criar uma reserva" label="Criar" value="Criar"
                             icon="icon-criar" oncomplete="dialogCriarReserva.show()"/>
        </h:form>

        <h:form id="formDataTableReserva" rendered="#{(loginBean.exibeFuncaoAdm) or (loginBean.exibeFuncaoReserva)}">

            <center>
                <h2>
                    <p:outputLabel value="Lista de reservas vazia!!" rendered="#{empty reservaBean.lista}"/>
                </h2>
            </center>

            <center>
                <p:dataTable id="dataTableReserva" selectionMode="single" paginator="true" rows="10" style="width: 90%"
                             paginatorPosition="bottom" paginatorTemplate="{CurrentPageReport} {FirstPageLink}
                             {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                             paginatorAlwaysVisible="false"
                             rendered="#{not empty reservaBean.lista}"
                             rowKey="#{reserva.codReserva}" selection="#{reservaBean.reservaSelecionada}"
                             var="reserva" value="#{reservaBean.lista}">

                    <f:facet name="header">  
                        Lista de Reservas  
                    </f:facet>

                    <p:column headerText="Código da Reserva" style="width:24%">
                        <h:outputText value="#{reserva.codReserva}"/>
                    </p:column>

                    <p:column headerText="Nome do recurso" style="width:24%">
                        <h:outputText value="#{reserva.recurso.nome}"/>
                    </p:column>

                    <p:column headerText="Nome do Usuário" style="width:24%">
                        <h:outputText value="#{reserva.usuario.nomeUsuario}"/>
                    </p:column>

                    <p:column headerText="Data" style="width:24%">
                        <h:outputText value="#{reserva.dataReserva}">
                            <f:convertDateTime dateStyle="medium"/>
                        </h:outputText>
                    </p:column>

                    <p:column headerText="Hora Início" style="width:24%">
                        <h:outputText value="#{reserva.horaInicio}">
                            <f:convertDateTime pattern="HH:mm"/>
                        </h:outputText>
                    </p:column>

                    <p:column headerText="Hora Fim" style="width:24%">
                        <h:outputText value="#{reserva.horaFim}">
                            <f:convertDateTime pattern="HH:mm"/>
                        </h:outputText>
                    </p:column>

                    <p:column headerText="Ações" style="width:4%">
                        <p:panelGrid columns="2">

                            <p:commandButton id="btnAlterarReserva" title="Alterar a reserva" label="Alterar" value="Alterar"
                                             icon="icon-editar" oncomplete="dialogAlterarReserva.show()"
                                             update=":formAlterarReserva"
                                             rendered="#{(loginBean.exibeFuncaoAdm)or(loginBean.usuario.nomeUsuario eq reserva.usuario.nomeUsuario)}">
                                <f:setPropertyActionListener value="#{reserva}" target="#{reservaBean.reservaSelecionada}"/>
                            </p:commandButton>

                            <p:commandButton id="btnExcluirReserva" title="Excluir a reserva" label="Excluir" value="Excluir"
                                             icon="icon-excluir" oncomplete="dialogExcluirReserva.show()"
                                             update=":formExcluirReserva"
                                             rendered="#{(loginBean.exibeFuncaoAdm)or(loginBean.usuario.nomeUsuario eq reserva.usuario.nomeUsuario)}">
                                <f:setPropertyActionListener value="#{reserva}" target="#{reservaBean.reservaSelecionada}"/>
                            </p:commandButton>

                        </p:panelGrid>
                    </p:column>

                </p:dataTable>
            </center>                    
        </h:form>

        <h:form id="formCriarReserva">
            <p:dialog id="dialogCriarReserva" header="Criar Reserva" widgetVar="dialogCriarReserva" resizable="false"
                      showEffect="size" hideEffect="blind" modal="true" draggable="false">
                <h:panelGrid id="panelGridCriarReserva" columns="2" cellpadding="4" style="margin:0 auto;">

                    <h:outputLabel for="nomeUsuarioCriarReserva" value="Nome Usuário:"/>
                    <p:outputLabel id="nomeUsuarioCriarReserva" value="#{loginBean.usuario.nomeUsuario}"/>

                    <h:outputLabel for="nomeRecursoCriarReserva" value="* Nome do recurso:"/>
                    <p:selectOneMenu id="nomeRecursoCriarReserva" required="true"
                                     requiredMessage="Favor informar o nome do recurso para a reserva."
                                     value="#{reservaBean.reservaSelecionada.recurso.nome}">  
                        <f:selectItem itemLabel="- Selecione -" itemValue=""/>  
                        <f:selectItems value="#{recursoBean.selecionarUmItemRecurso}"/>
                        <p:message for="nomeRecursoCriarReserva"/>
                    </p:selectOneMenu>

                    <h:outputLabel for="dataCriarReserva" value="* Data:"/>
                    <p:calendar id="dataCriarReserva" locale="pt_BR" title="Data da reserva" label="Data"
                                effect="size" showButtonPanel="true" required="true"
                                requiredMessage="Favor informar a data da reserva."
                                value="#{reservaBean.reservaSelecionada.dataReserva}">
                        <p:watermark for="dataCriarReserva" value="Data da reserva do recurso"/>
                        <p:message for="dataCriarReserva"/>
                    </p:calendar>

                    <h:outputLabel for="horaIniCriarReserva" value="* Hora início:"/>
                    <p:calendar id="horaIniCriarReserva" title="Hora inicial da reserva" label="Inicial" timeOnly="true"
                                effect="size" required="true" pattern="HH:mm" minHour="8" maxHour="16"
                                requiredMessage="Favor informar a hora inicial da reserva."
                                value="#{reservaBean.reservaSelecionada.horaInicio}">
                        <p:watermark for="horaIniCriarReserva" value="Horário inicial da reserva do recurso"/>
                        <p:message for="horaIniCriarReserva"/>
                    </p:calendar>

                    <h:outputLabel for="horaFimCriarReserva" value="* Hora fim"/>
                    <p:calendar id="horaFimCriarReserva" title="Hora final da reserva" label="Final" timeOnly="true"
                                effect="size" required="true" pattern="HH:mm" minHour="8" maxHour="19"
                                requiredMessage="Favor informar a hora final da reserva."
                                value="#{reservaBean.reservaSelecionada.horaFim}">
                        <p:watermark for="horaFimCriarReserva" value="Horário final da reserva do recurso"/>
                        <p:message for="horaFimCriarReserva"/>
                    </p:calendar>

                    <f:facet name="footer">
                        <p:separator/>
                        <center>
                            <p:commandButton id="btnAceitarCriarReserva" title="Salvar a reserva" label="Salvar" value="Salvar"
                                             icon="icon-salvar" oncomplete="dialogCriarReserva.hide()"
                                             update=":growlReserva, :formDataTableReserva"
                                             validateClient="true"
                                             actionListener="#{reservaBean.btnCriarReserva(actionEvent)}"/>
                            <p:commandButton id="btnCancelarCriarReserva" title="Cancelar" label="Cancelar" value="Cancelar"
                                             icon="icon-cancelar" oncomplete="dialogCriarReserva.hide()"/>
                        </center>
                    </f:facet>

                </h:panelGrid>
            </p:dialog>
        </h:form>

        <h:form id="formAlterarReserva">
            <p:dialog id="dialogAlterarReserva" header="Alterar Reserva" widgetVar="dialogAlterarReserva" resizable="false"
                      showEffect="fade" hideEffect="explode" modal="true" draggable="false">
                <h:inputHidden value="#{reservaBean.reservaSelecionada.codReserva}"/>
                <h:inputHidden value="#{reservaBean.reservaSelecionada.recurso.nome}"/>
                <h:panelGrid id="panelGridAlterarReserva" columns="2" cellpadding="4" style="margin:0 auto;">

                    <h:outputLabel for="nomeUsuarioAlterarReserva" value="Nome Usuário:"/>
                    <p:outputLabel id="nomeUsuarioAlterarReserva" value="#{loginBean.usuario.nomeUsuario}"/>

                    <h:outputLabel for="nomeRecursoAlterarReserva" value="Nome do recurso:"/>
                    <h:outputText id="nomeRecursoAlterarReserva" value="#{reservaBean.reservaSelecionada.recurso.nome}"/>

                    <h:outputLabel for="dataAlterarReserva" value="* Data:"/>
                    <p:calendar id="dataAlterarReserva" locale="pt_BR" title="Data da reserva" label="Data"
                                effect="size" showButtonPanel="true" required="true"
                                requiredMessage="Favor informar a data da reserva."
                                value="#{reservaBean.reservaSelecionada.dataReserva}">
                        <p:watermark for="dataAlterarReserva" value="Data da reserva do recurso"/>
                        <p:message for="dataAlterarReserva"/>
                    </p:calendar>

                    <h:outputLabel for="horaIniAlterarReserva" value="* Hora início:"/>
                    <p:calendar id="horaIniAlterarReserva" title="Hora inicial da reserva" label="Inicial" timeOnly="true"
                                effect="size" required="true" pattern="HH:mm" minHour="8" maxHour="16"
                                requiredMessage="Favor informar a hora inicial da reserva."
                                value="#{reservaBean.reservaSelecionada.horaInicio}">
                        <p:watermark for="horaIniAlterarReserva" value="Horário inicial da reserva do recurso"/>
                        <p:message for="horaIniAlterarReserva"/>
                    </p:calendar>

                    <h:outputLabel for="horaFimAlterarReserva" value="* Hora fim"/>
                    <p:calendar id="horaFimAlterarReserva" title="Hora final da reserva" label="Final" timeOnly="true"
                                effect="size" required="true" pattern="HH:mm" minHour="8" maxHour="19"
                                requiredMessage="Favor informar a hora final da reserva."
                                value="#{reservaBean.reservaSelecionada.horaFim}">
                        <p:watermark for="horaFimAlterarReserva" value="Horário final da reserva do recurso"/>
                        <p:message for="horaFimAlterarReserva"/>
                    </p:calendar>

                    <f:facet name="footer">
                        <p:separator/>
                        <center>
                            <p:commandButton id="btnAceitarAlterarReserva" title="Alterar a reserva" label="Alterar" value="Alterar"
                                             icon="icon-salvar" oncomplete="dialogAlterarReserva.hide()"
                                             update=":growlReserva, :formDataTableReserva"
                                             validateClient="true"
                                             actionListener="#{reservaBean.btnAlterarReserva(actionEvent)}"/>
                            <p:commandButton id="btnCancelarAlterarReserva" title="Cancelar a alteração" label="Cancelar" value="Cancelar"
                                             icon="icon-cancelar" oncomplete="dialogAlterarReserva.hide()"/>
                        </center>
                    </f:facet>

                </h:panelGrid>
            </p:dialog>
        </h:form>

        <h:form id="formExcluirReserva">
            <p:confirmDialog id="confirmDialogReserva" header="Exclusão de Reserva" widgetVar="dialogExcluirReserva"
                             showEffect="bounce" hideEffect="drop" severity="alert"
                             message="Tem certeza que deseja excluir a reserva do recurso #{reservaBean.reservaSelecionada.recurso.nome}?">
                <h:inputHidden value="#{reservaBean.reservaSelecionada.codReserva}"/>

                <center>
                    <p:commandButton id="confirmarExclusaoReserva" title="Confirmar a exclusão da reserva" label="Excluir" value="Confirmar"
                                     icon="icon-confirmar" oncomplete="dialogExcluirReserva.hide()"
                                     update=":growlReserva, :formDataTableReserva"
                                     actionListener="#{reservaBean.btnExcluirReserva(actionEvent)}"/>
                    <p:commandButton id="cancelarExclusaoReserva" title="Cancelar a exclusão da reserva" label="Cancelar" value="Cancelar"
                                     icon="icon-cancelar" oncomplete="dialogExcluirReserva.hide()"/>
                </center>

            </p:confirmDialog>
        </h:form>

    </ui:define>

</ui:composition>
